﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-06-09 11:14:13
 * 功能：Android平台原生交互接口
**************************************************************************************/

#if UNITY_ANDROID
using DataEyeAnalytics;
using Game.Framework;
using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using UnityEngine.Networking;

public class AndroidNative : INativeAPI
{
    private AndroidJavaClass m_Ajc;
    private AndroidJavaObject m_Ajo;

    private AndroidNative()
    {
#if !UNITY_EDITOR
        m_Ajc = new AndroidJavaClass("com.android.nativ.Interaction");
        m_Ajo = m_Ajc.CallStatic<AndroidJavaObject>("getInstance");
#endif
    }

    /// <summary>触发震动反馈</summary>
    public void TriggerVibration(VibrationType type)
    {
        if (m_Ajo != null)
        {
            m_Ajo.Call("triggerVibration", (int)type);
        }
        LogManager.Log($"触发移动端震动效果：{type}");
    }

    public bool HasSimCard()
    {
        if (m_Ajo != null)
        {
            return m_Ajo.Call<bool>("hasSimCard");
        }
        return false;
    }

    public void ShowIntAd()
    {
        AdMgr.Instance.ShowInt(GameDataMgr.Instance.TriggerAdType);
    }

    public void ShowRewardAd()
    {
        AdMgr.Instance.ShowReward(GameDataMgr.Instance.TriggerAdType);
    }

    public void SendBILog(string key, string value)
    {
        var properties = JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
        DataEyeAnalyticsAPI.Track(key, properties);
        DataEyeAnalyticsAPI.Flush();
        LogManager.Log($"bi, eventName:{key} dataCount:{properties.Count} data:{value}", "upload");
    }

    public void SetUserProperty(string value)
    {
        var properties = JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
        DataEyeAnalyticsAPI.UserSet(properties);
        LogManager.Log($"设置用户属性，{value}");
    }

    public string GetDeviceId()
    {
        string result = GameDataMgr.Instance.Uid.ToString();
        if (m_Ajo != null)
        {
            result = m_Ajo.Call<string>("getDeviceId");
        }
        LogManager.Log($"设备ID：{result}");
        return result;
    }

    public string GetAppToken()
    {
        string result = "";
        if (m_Ajo != null)
        {
            result = m_Ajo.Call<string>("getAppToken");
        }
        LogManager.Log($"ApToken：{result}");
        return result;
    }

    public void ReportToH5(string data)
    {
        throw new NotImplementedException();
    }

    public void ShowIntAd_Self()
    {
        throw new NotImplementedException();
    }

    public void ShowRewardAd_Self()
    {
        throw new NotImplementedException();
    }

    public void ShowSelfntAd()
    {
        throw new NotImplementedException();
    }

    public void ShowSelfRewardAd()
    {
        throw new NotImplementedException();
    }

    public string GetIdfa()
    {
        throw new NotImplementedException();
    }

    public void SaveImageToAlbum(string path)
    {
        if (m_Ajo != null)
        {
            m_Ajo.Call("saveImage", path);
        }
    }

    public void SaveVideoToAlbum(string path)
    {
        if (m_Ajo != null)
        {
            m_Ajo.Call("saveVideo", path);
        }
    }

    public void Clear()
    {
        if (m_Ajc != null)
        {
            m_Ajc.Dispose();
            m_Ajc = null;
        }

        if (m_Ajo != null)
        {
            m_Ajo.Dispose();
            m_Ajo = null;
        }
    }
}
#endif